<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" align-center>
		<el-container>
			<el-header>
				<div class="left-panel">
					<!-- <el-button type="primary" @click="back">确定</el-button> -->
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-date-picker v-model="search.create_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期"
										end-placeholder="结束日期"/>
						<el-input v-model="search.code" placeholder="采购单号" clearable @keyup.enter="upsearch"></el-input>
						<el-select v-model="search.apply_id" placeholder="请选择供应商" filterable clearable>
							<el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
						</el-select>
						<el-select v-model="search.purchase_staff" filterable clearable placeholder="请选择采购人">
							<el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
						</el-select>
						<el-select v-model="search.is_ticket" placeholder="是否开票" filterable clearable>
							<el-option label="否" :value="1"></el-option>
							<el-option label="是" :value="2"></el-option>
						</el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" row-key="id" :apiObj="apiObj" highlight-current-row @row-click="selectionChange" stripe remoteSort remoteFilter border height="650"
						 :params="query">
					<el-table-column type="index" width="50" align="center"></el-table-column>
					<el-table-column label="采购单号" prop="code" align="center" width="130"></el-table-column>
					<el-table-column label="供应商" prop="apply.apply_name" align="center" width="130"></el-table-column>
					<el-table-column label="开单日期" prop="open_date" align="center" width="100"></el-table-column>
					<el-table-column label="数量" prop="number_all" align="center" width="80"></el-table-column>
					<el-table-column label="金额" prop="price_all" align="center" width="80"></el-table-column>
					<el-table-column label="是否开票" prop="is_ticket" align="center" width="80">
						<template #default="scope">
							<el-tag v-if="scope.row.is_ticket ==2" type="success">是</el-tag>
							<el-tag v-else type="danger">否</el-tag>
						</template>
					</el-table-column>
					<!--				<el-table-column label="是否含税" prop="is_tax" align="center" width="80">-->
					<!--					<template #default="scope">-->
					<!--						<el-tag v-if="scope.row.is_tax == 1" type="success">是</el-tag>-->
					<!--						<el-tag v-else type="danger">否</el-tag>-->
					<!--					</template>-->
					<!--				</el-table-column>-->
					<el-table-column label="采购人" prop="purchase.name" align="center" width="100"></el-table-column>
					<el-table-column label="制单人" prop="admin.name" align="center" width="100"></el-table-column>
					<el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
					<el-table-column label="审核状态" prop="status" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.status == 0">{{ scope.row.status_text }}</el-tag>
							<el-tag v-if="scope.row.status == 1" type="success">{{ scope.row.status_text }}</el-tag>
							<el-tag v-if="scope.row.status == 2" type="danger">{{ scope.row.status_text }}<span v-if="scope.row.reason">：</span>{{ scope.row.reason }}</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="备注" prop="remark" align="center"></el-table-column>
				</scTable>
			</el-main>
		</el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	props: {
		query: {type: Object, default: () => ({})},
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择采购单',
				edit: '选择采购单',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			apiObj: this.$API.procure.order.list,
			selection: [],
			search: {
				code: null,
				apply_id: null,
				purchase_staff: null,
				is_ticket: null,
				is_tax: null,
				create_time: null
			},
		}
	},
	mounted() {
		// this.getShop()
		this.getApply();
		this.getAdmin();
	},
	methods: {
		/**供应商*/
		async getApply() {
			var res = await this.$API.apply.apply.all.get();
			this.applyList = res.data;
		},
		/**获取采购人*/
		async getAdmin() {
			var res = await this.$API.system.admin.all.get();
			this.adminList = res.data;
		},
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 选择事件
		selectionChange(selection) {
			// this.selection = selection;
			this.$emit('success', selection)
			this.visible = false;
		},
		// 选中返回
		back() {
			this.$emit('success', this.selection, this.type)
			this.visible = false;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
	}
}
</script>

<style></style>
